﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using BaseClasses;
using Daystate.ConfigurationManager;

namespace Djikstria
{

    public class GraphBuilder
    {
        public static Graph CreateGraph()
        {
            Graph graph = new Graph();

            string sheetsPath = Utilities.Configuration[Configuration.ConfigKeys.GraphDescriptionFilePath].GetValue<string>();
            string noLinkValue = Utilities.Configuration[Configuration.ConfigKeys.NoLinkMark].GetValue<string>();

            ExcelReader ExSheet = null;
            try
            {
                ExSheet = new ExcelReader(sheetsPath);

                int columnCounter;
                int rowCounter;
                int NumberColums = ExSheet.GetRange();
                int temp = 2;

                for (columnCounter = 1; columnCounter < NumberColums; columnCounter++)
                {
                    for (rowCounter = temp; rowCounter <= NumberColums; rowCounter++)
                    {
                        string cellValue = ExSheet.GetCellValue(rowCounter, columnCounter).ToString();

                        if (!string.IsNullOrEmpty(cellValue))
                        {
                            if (cellValue != noLinkValue)
                            {
                                int linkValue = Convert.ToInt32(cellValue);
                                if (linkValue > 0)
                                {
                                    // Assaign Alfa vertex
                                    string alfaSymbol = ExSheet.GetHeader(columnCounter);
                                    Vertex alfaVertex = null;
                                    if (graph.Any(x => x.Alfa.Symbol == alfaSymbol))
                                        alfaVertex = graph.First(x => x.Alfa.Symbol == alfaSymbol).Alfa;
                                    else if (graph.Any(x => x.Omega.Symbol == alfaSymbol))
                                        alfaVertex = graph.First(x => x.Omega.Symbol == alfaSymbol).Omega;
                                    else
                                        alfaVertex = new Vertex(alfaSymbol);

                                    // Assaign Omega
                                    string omegaSymbol = ExSheet.GetHeader(rowCounter);
                                    Vertex omegaVertex = null;
                                    if (graph.Any(x => x.Omega.Symbol == omegaSymbol))
                                        omegaVertex = graph.First(x => x.Omega.Symbol == omegaSymbol).Omega;
                                    else if (graph.Any(x => x.Alfa.Symbol == omegaSymbol))
                                        omegaVertex = graph.First(x => x.Alfa.Symbol == omegaSymbol).Alfa;
                                    else
                                        omegaVertex = new Vertex(omegaSymbol);


                                    graph.Add(new Link(alfaVertex, omegaVertex, linkValue));
                                }
                                else
                                    throw new Exception("Link value less or equal 0");
                            }
                        }
                        else
                            break;
                    }
                    temp++;
                }

                return graph;

            }
            finally
            {
                if (ExSheet != null)
                {
                    ExSheet.CloseWorksheet();
                }
            }
        }
    }


    //public class GraphBuilder
    //{
    //    /// <summary>
    //    /// Loads up delivered *.xlsx file with graph description and creates graph instance.
    //    /// </summary>
    //    /// <returns></returns>
    //    public static Graph PrepareGraph()
    //    {
    //        Graph graph = new Graph();

    //        ExcelReader reader = new ExcelReader(Constants.filePath);

    //        int columnCounter; // loop #1
    //        int rowCounter; // loop #2
    //        int maxColumnIterations = 20; // TODO przenieść do konfiguracji 
    //        int maxRowsIterations = maxColumnIterations; // TODO przenieść do konfiguracji 
    //        char startColumnId = Constants.StartColumnLetter;
    //        char columnId;

    //        for (columnCounter = 1; columnCounter < maxColumnIterations; columnCounter++)
    //            for (rowCounter = 2; rowCounter < maxRowsIterations; rowCounter++)
    //            {
    //                columnId = Convert.ToChar(startColumnId + columnCounter);

    //                string cellValue = reader.GetCellValue(Convert.ToString(columnId), rowCounter);

    //                if (!string.IsNullOrEmpty(cellValue))
    //                {
    //                    if (cellValue != Constants.NoLinkMark)
    //                    {
    //                        int linkValue = int.Parse(cellValue);
    //                        if (linkValue > 0)
    //                        {
    //                            graph.Add(new Link(
    //                                new Vertex(columnId),
    //                                new Vertex(Convert.ToChar(rowCounter + startColumnId)),
    //                                linkValue));
    //                        }
    //                    }
    //                }
    //                else
    //                {
    //                    maxColumnIterations = rowCounter;
    //                    break; // Skip to next column
    //                }
    //            }
    //        return graph;
    //    }

    //    class Constants
    //    {
    //        public const char StartColumnLetter = 'A';
    //        public const char EndColumnLetter = 'Z';

    //        public static string filePath = Utilities.Configuration[Configuration.ConfigKeys.GraphDescriptionFilePath].GetValue<string>();
    //        public static string NoLinkMark = Utilities.Configuration[Configuration.ConfigKeys.NoLinkMark].GetValue<string>();

    //    }
    //}
}
